<?php
namespace Home\Controller;
use Think\Controller;

class UsercenterController extends HomeController
{
    //用户中心首页
    public function index()
    {
        $this->assign('title', "用户中心");

        //用户总积分
        $user_id = $_SESSION['home']['id'];
        $res = M('integral')->where("user_id='$user_id'")->find();
//        dump($res);die;


        $this->assign('res',$res);
        $this->display("Usercenter/index");
    }

    //优惠券首页
    public function quan()
    {
        $this->assign('title', "我的优惠券");
        $id = $_SESSION['home']['id'];
        $ms = M('user_coupon');
        $Model = new \Think\Model();
        //未失效  is_lose=1
        $list1 = $Model->table('shopping_coupon AS c,shopping_user_coupon AS uc')->where("c.id = uc.coupon_id AND user_id=$id AND uc.is_lose=1")->select();
        //失效  is_lose=0
        $list2 = $Model->table('shopping_coupon AS c,shopping_user_coupon AS uc')->where("c.id = uc.coupon_id AND user_id=$id AND uc.is_lose=0")->select();
        $today = mktime();//即时时间戳
        foreach ($list1 as $k => $val) {
            $end_time = strtotime($list1[$k]['end_time']);
            $id = $list1[$k]['id'];
            if ($end_time <= $today) {
//                $ms = M("coupon"); // 实例化User对象
                $data['is_lose'] = 0;
                $res1 = $ms->where("id='$id'")->save($data);
            } else {
                $data['is_lose'] = 1;
                $res1 = $ms->where("id='$id'")->save($data);
            }
        }
        foreach ($list2 as $k => $val) {
            $end_time = strtotime($list2[$k]['end_time']);
            $id = $list2[$k]['id'];
            if ($end_time <= $today) {
//                $ms = M("coupon"); // 实例化User对象
                $data['is_lose'] = 0;
                $res1 = $ms->where("id='$id'")->save($data);
            } else {
                $data['is_lose'] = 1;
                $res1 = $ms->where("id='$id'")->save($data);
            }
        }
        $this->assign('list1', $list1);
        $this->assign('list2', $list2);
        $this->display("Usercenter/quan");
    }

    //用户信息页
    public function userinfo()
    {
        $this->assign('title', '用户中心');
        $id = $_SESSION['home']['id'];//用户id
        $ms = M('user');
        $res = $ms->where("id='$id'")->find();
        $this->assign('res', $res);
        $this->display('Usercenter/userinfo');
    }

    //修改用户信息
    public function userinfo_edit()
    {
        $id = $_SESSION['home']['id'];//用户id
        $ms = M("user"); // 实例化User对象
        $data['name'] = $_POST['name'];
        $data['tel'] = $_POST['tel'];
        $data['sex'] = $_POST['sex'];
        $data['email'] = $_POST['email'];
        $res = $ms->where("id='$id'")->save($data);
//        dump($res);die;
        if ($res) {
            $name = $_POST['name'];
            session('name', $name);

            $this->success('修改成功', U('Usercenter/userinfo'));
        } else {
            $this->error('修改失败', U('Usercenter/userinfo'));
        }
    }

    //修改密码首页
    public function pwd_edit()
    {
        $this->assign('title', '修改密码');
        $this->display('Usercenter/pwd_edit');
    }

    //修改密码
    public function pwd_editdo()
    {
        $ms = M("user"); // 实例化User对象
        $id = $_SESSION['home']['id'];//用户id
        $data['pwd'] = md5($_POST['pwd']);
        $res = $ms->where("id='$id'")->save($data);
//        dump($res);die;
        if ($res) {
            session("home", null);
//            $this->success('退出成功',U('Index/index'));
            $this->success('修改成功,重新登录', U('User/login'));
        } else {
            $this->error('修改失败', U('Usercenter/userinfo'));
        }

    }

    //地址列表
    public function address()
    {
        $this->assign('title', '收货地址管理');
        $id = $_SESSION['home']['id'];
        $list = M('useradd')->where("user_id='$id'")->order('`is_default` desc')->select();
//dump($list);die;
        $this->assign('list', $list);
        $this->display('Usercenter/address');
    }
    //修改地址
    public function addressdo()
    {
        $data['name'] = I('post.name');
        $data['tel'] = I('post.tel');
        $data['address'] = I('post.address');
        $data['create_time'] = date('Y-m-d H:i:s');
        $data['user_id'] = $_SESSION['home']['id'];
        $res = M('useradd')->add($data);
//        dump($res);die;
        if ($res) {
//            session("home",null);
//            $this->success('退出成功',U('Index/index'));
            $this->success('添加成功', U('Usercenter/address'));
        } else {
            $this->error('添加失败', U('Usercenter/address'));
        }
    }
    //删除地址
    public function addressdel()
    {
        if (IS_GET) {
            $id = I('get.id', 0, 'int');
            if (M('useradd')->delete($id) > 0) {
                $this->success('删除地址成功', U('Usercenter/address'));
            } else {
                $this->error('删除地址失败', U('Usercenter/address'));
            }
        }
    }
    //设置首选地址
    public function addressone()
    {
        //判断是否有首选地址
        $user_id = $_SESSION['home']['id'];
        $setadd_id = $_GET['id'];//要设置地址的id
        $ms = M('useradd');
        $lis = $ms->where("user_id='$user_id'"."  AND `is_default`=1")->find();
        if($lis){
            //已存在首选地址,则取消首选地址,
            $add_id = $lis['id'];//查出来的首选的地址的id
            $data['is_default'] = 0;
            $res = $ms->where("user_id='$user_id' AND id= '$add_id'")->save($data);
        }
            //设置为首选地址
            $data['is_default'] = 1;
            $res = $ms->where("user_id='$user_id' AND id= '$setadd_id'")->save($data);
            if ($res) {
                header("Location:".$_SERVER['HTTP_REFERER']);//改变后返回
            } else {
                $this->error('设置失败', U('Usercenter/address'));
            }

    }
    //我的订单
    public function myorder(){
        $this->assign('title','我的订单');
        $this->isLogin();
        $userEvent = A("User","Event");
        //订单
        $orderList = $userEvent->queryMyOrder();
        $this->assign("orderList",$orderList);
        //订单详情
        $orderDetailList = $userEvent->queryMyOrderDetail($orderList);
        $this->assign("orderDetailList",$orderDetailList);
        $this->display('Usercenter/myorder');
    }
    //我的团购订单
    public function grouporder(){
        $this->assign('title','团购订单');
        //根据用户id查出他的团购订单
        $usercenterEvent = A("Usercenter","Event");
        $groupList = $usercenterEvent->grouporder();
        $this->assign("groupList",$groupList);
        $this->display('Usercenter/grouporder');
    }
    //我的抢购订单
    public function seckillorder(){
        $this->assign('title','秒杀订单');
        $this->display('Usercenter/seckillorder');
    }
//    我的评价
    public function myevaluate(){
        $this->assign('title','我的评价');
        $id = $_SESSION['home']['id'];
        $queryeva = M('evaluate')
//            ->fetchSql()
            ->field("g.name,e.msg,e.grade,e.create_time")
            ->alias('e')
            ->join('shopping_goods as g on g.id=e.goods_id')
            ->where("e.user_id=$id")->select();
        $this->assign("queryeva",$queryeva);
//        dump($queryeva);die;
        $this->display('Usercenter/myevaluate');
    }


    //我的订单页面
    public function showOrder ()
    {
        $this->isLogin();
        $this->assign("title","我的订单");
        $userEvent = A("User","Event");
        //订单
        $orderList = $userEvent->queryMyOrder();
        $this->assign("orderList",$orderList);
        //订单详情
        $orderDetailList = $userEvent->queryMyOrderDetail($orderList);
        $this->assign("orderDetailList",$orderDetailList);
        $this->display("Usercenter/myorder");
    }

    //从我的订单页面付款
    public function pay ()
    {
        $usercenterEvent = A("Usercenter","Event");
        $res = $usercenterEvent -> commitOrder();
        $this->assign("title","付款");
        $this->assign("payInfo",$res);
        $this->display("User/pay");


    }

    //添加评论
    public function addeva(){
        if(IS_GET){
            $goods_id = I("get.goods_id");
            $order_id = I("get.order_id");
            $this->assign("goods_id",$goods_id);
            $this->assign("order_id",$order_id);
            $this->display('Usercenter/addeva');
        }
    }
    public function addevado(){
//        dump($_POST);die;
        $data['user_id'] = $_SESSION['home']['id'];
//        dump($user_id);die;
        $data['goods_id'] = $_POST['goods_id'];

        $data['msg'] = $_POST['msg'];
        $data['grade'] = $_POST['grade'];
        $data['create_time'] = date('Y-m-d H:i:s');
        $res = M('evaluate')->add($data);
        if($res>0){
            //评价成功之后,把订单详情中的该订单下的is_evaluate改为1
            $order_id = $_POST['order_id'];
            $goods_id = $_POST['goods_id'];

            $orderDetail = M("orderdetail");
            $orderDetail->where("order_id='$order_id' and goods_id=$goods_id")->setField("is_evaluate",1);
            $this->success("评价成功",U('Usercenter/myevaluate'));
        }else{
            $this->error("评价失败",U('Usercenter/addevado'));
        }
    }
    //积分详情
    public function integinfo(){
        $this->assign('title','积分详情');
        $user_id = $_SESSION['home']['id'];
        $res = M('integraldetail')->alias("ind")->join("shopping_integral as ing on ing.integral_id=ind.integral_id")->where("ing.user_id='$user_id'")->select();
//        dump($res);die;

        $this->assign('res',$res);
        $this->display('Usercenter/integinfo');
    }

    //头像修改页面
    public function headImg()
    {
        $this->assign("title","头像修改");
        $this->display("Usercenter/headImg");
    }

    //头像修改
    public function updateImg()
    {
        $usercenterEvent = A("Usercenter","Event");
        $row = $usercenterEvent -> updateImg();
        if($row > 0){
            $this->success("头像修改成功");
        }else{
            $this->error("头像修改失败");
        }
    }


    //确认收货
    public function receiveGood()
    {
        if(IS_GET){
            $usercenterEvent = A("Usercenter","Event");
            $row = $usercenterEvent -> receiveGood();
            if($row > 0){
                $this->success("收货成功");
            }else{
                $this->error("收货失败");
            }
        }
    }
}